<?php
/**
 *
 *
 *
 */
namespace Yz\modules\Uploads;

class Model_Upload
    extends \Yz_Model_Abstract
{
    const UPLOAD_ACTION = 'Upload';
    const UPLOAD_CONTROLLER = 'Upload';
    const UPLOAD_PATH   = 'www/uploads';

    protected $uploadPath;


    /** Model's fields definition
     */
    protected function init()
    {
        debug();
        parent::init();
        $d =& $this->__dict__;
        $d['id']           = new \Yz_Property_Id();
        $d['filename']     = new \Yz_Property_String();
        $d['originalname'] = new \Yz_Property_String();
        $d['extension']    = new \Yz_Property_String();
        $d['created']      = new \Yz_Property_Timestamp();
        $d['filesize']     = new \Yz_Property_Number();
        return $this;
    }


    /**************************
     *      Public methods    *
     **************************/

    /**
     *
     *  @return Item
     *  @throw
     */
    public function processUpload($uploadName)
    {
        $action = $this
            ->selectAction(static::UPLOAD_ACTION)
            ->setUploadName($uploadName)
        ;
        return $this->_process($action);
    }


    public function getUploadPath()
    {
        $uploadPath =& $this->uploadPath;
        if (null === $uploadPath)
            $uploadPath = static::UPLOAD_PATH;

        return $uploadPath;
    }

    /*****************************
     *      Protected methods    *
     *****************************/

    protected function _process($action)
    {
        $uploadPath = $this->getUploadPath();
        $values = $action
            ->setUploadPath($uploadPath)
            ->execute()
            ->fetch()
        ;
        $item = $this->newItem();
        foreach ($values as $propName => $v)
            $item[$propName] = $v;
        return $item;
    }

}
